// zh_cjdianc/shopList/reserve.js
var a = getApp(), util = require("../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2018-09-30',
    time: '12:00',
    array: [],
    type:'',
    mobile:'',
    name:'',
    peoplenum:'',
    desc:'',
    index: 0,
    index2:0,
    people:['1人','2人','3人','4人','5人','6人及以上'],
    price:[],
    popErrorMsg:'',
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var a = this.data.array;
    console.log(a);
    this.setData({
      index2: e.detail.value,
      type: a[e.detail.value]['id']
    })
  },
  bindPickerChange_people: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var a= 1,b=Number(e.detail.value),c;
    c = a + b;
    this.setData({
      peoplenum: c,
      index:e.detail.value
    })
  },
  tapdesc:function(e){
    console.log(e)
    this.setData({
      desc:e.detail.value
    })
  },
  tapmobile: function (e) {
    console.log(e)
    this.setData({
      mobile: e.detail.value
    })
  },
  tapname: function (e) {
    console.log(e)
    this.setData({
      name: e.detail.value
    })
  },
  goPaySuccess: function (e) {
    console.log(wx.getStorageSync('users'));
    var user_id = wx.getStorageSync('users').id;
    var n = this;
    var store_id = n.data.price['id'];
    var yd_date = n.data.date;
    var yd_time = n.data.time;
    var table_id = n.data.type;
    var name = n.data.name;
    var mobile = n.data.mobile;
    var people = n.data.peoplenum;
    var price = n.data.price['yd_price'];
    var desc = n.data.desc;
    if (!table_id) {
      n.setData(
        { popErrorMsg: "该商家位置已满" }
      )
      n.ohShitfadeOut();return;
    }
    if(!mobile || !name){
      n.setData(
        { popErrorMsg: "请输入联系人或电话" }
      )
      n.ohShitfadeOut(); return;
    }
    if (!(/^1[34578]\d{9}$/.test(mobile))) {
      n.setData(
        { popErrorMsg: "请输入正确的手机号码" }
      )
      n.ohShitfadeOut(); return;
    }
    if (name.length >20) {
      n.setData(
        { popErrorMsg: "请输入正确的用户名称" }
      )
      n.ohShitfadeOut(); return;
    }

    a.util.request({
      url: "entry/wxapp/newstoreydpay",
      cachetime: "0",
      data: { store_id: store_id, yd_date: yd_date, yd_time: yd_time, table_id: table_id, name: name, mobile: mobile, people: people, price: price, desc: desc, user_id: user_id },
      success: function (t) {
        console.log(t.data)
        if(t.data.code == 0){
          // var c = t.data.data.return;
          // wx.requestPayment(
          //   {
          //     'timeStamp': c.timeStamp,
          //     'nonceStr': c.nonceStr,
          //     'package': c.package,
          //     'signType': c.signType,
          //     'paySign': c.paySign,
          //     'success': function (res) { 
          //     },
          //     'fail': function (res) { },
          //     'complete': function (res) { }
          //   })
          wx.showLoading({ //需要调用隐藏方法
            title: '加载中...',
            mask: true
          })
          wx.reLaunch ({
            url: 'paySuccess?date=' + e.currentTarget.dataset.date + '&time=' + e.currentTarget.dataset.time + '&peoplenum=' + e.currentTarget.dataset.peoplenum + '&name=' + e.currentTarget.dataset.name,
            success: function (res) {
              wx.hideLoading() //隐藏加载框
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }else{
          n.setData(
            { popErrorMsg: t.data.message }
          )
          n.ohShitfadeOut(); return;
        }
      }
    });
  },
  ohShitfadeOut() {
    var fadeOutTimeout = setTimeout(() => {
      this.setData({ popErrorMsg: '' });
      clearTimeout(fadeOutTimeout);
    }, 3000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var n = this;
    a.setNavigationBarColor(this);
    var store_id = options.id;

    var timestamp = new Date() / 1000;
    console.log(timestamp);
    var tomorrow_timetamp = timestamp + 24 * 60 * 60;
    var n_to = tomorrow_timetamp * 1000;
    var tomorrow_date = new Date(n_to);
    var Y_tomorrow = tomorrow_date.getFullYear();
    var M_tomorrow = (tomorrow_date.getMonth() + 1 < 10 ? '0' + (tomorrow_date.getMonth() + 1) : tomorrow_date.getMonth() + 1);
    var D_tomorrow = tomorrow_date.getDate() < 10 ? '0' + tomorrow_date.getDate() : tomorrow_date.getDate();
    var date = Y_tomorrow + '-' + M_tomorrow + '-' + D_tomorrow;
    a.util.request({
      url: "entry/wxapp/newstoreyddetial",
      cachetime: "0",
      data: { store_id: store_id },
      success: function (t) {
        if (t.data.data.table.length == 0){
          n.setData(
            { popErrorMsg: "该商家位置已满" }
          )
          n.ohShitfadeOut();return;
        }
        console.log(t.data)
        n.setData({
          storeDetail: t.data.data,
          array:t.data.data.table,
          price:t.data.data.price,
          type: t.data.data.table[0]['id'],
          peoplenum:1,
          date:date
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})